import gtk, os
import utils
import GMM_timeline.frame as frame
from effect_list import effect_list

max_width=400
max_height=300

class edit_text(gtk.Frame):
    def __init__(self, text, save, delete):
        gtk.Frame.__init__(self)
        self.save=save
        self.delete_text=delete
        self.set_label("Edit text: " + utils.get_name(text.comment))
        
        self.text=text
        ScrolledWindow=gtk.ScrolledWindow()
        ScrolledWindow.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
        HBox=gtk.VBox()
        OptionBox=gtk.Frame()
        OptionBox.set_label("Options:")
        VBox=gtk.VBox(spacing=5)
        OptionBox.add(VBox)
        
        self.image=gtk.Image()
        self.pixbuf=text.pixbuf
        width=self.pixbuf.get_width()
        height=self.pixbuf.get_height()
        
        if width > max_width or (width<max_width and height<max_height):
                height=height*max_width/width
                width=max_width
                if height > max_height:
                    width=width*max_height/height
                    height=max_height
        self.pixbuf = self.pixbuf.scale_simple(width, height, gtk.gdk.INTERP_HYPER)
        
        self.image.set_from_pixbuf(self.pixbuf)
        HBox.pack_start(self.image)
        HBox.pack_end(OptionBox, False)
        
        ##OptionBox
        VBox.pack_start(gtk.Label("Lenght"), False)
        #len
        self.len=gtk.SpinButton()
        self.len.set_range(1, 1000)
        self.len.set_increments(1, 10)
        self.len.set_value(self.text.len)
        self.len.connect("changed", self.change)
        VBox.pack_start(self.len, False)
        VBox.pack_start(gtk.Label("Text"), False)
        self.text_box=gtk.Entry()
        self.text_box.connect("changed", self.change)
        self.text_box.connect("activate", self.repaint_preview)
        self.text_box.set_text(self.text.comment)
        VBox.pack_start(self.text_box, False)
        #Elimina
        DeleteButton=gtk.Button("Delete")
        DeleteButton.connect("clicked", self.delete)
        VBox.pack_start(DeleteButton, False)
                
        ScrolledWindow.add_with_viewport(HBox)
        self.add(ScrolledWindow)
    
    def delete(self, obj):
        self.delete_text()
    
    def change(self, obj=None, event=None):
        self.text.len=self.len.get_value_as_int()
        self.text.set_comment(self.text_box.get_text())
        self.save()
    
    def repaint_preview(self, obj=None):
        self.change()
        self.text.redraw_picture()
        self.pixbuf=self.text.pixbuf
        width=self.pixbuf.get_width()
        height=self.pixbuf.get_height()
        
        if width > max_width or (width<max_width and height<max_height):
                height=height*max_width/width
                width=max_width
                if height > max_height:
                    width=width*max_height/height
                    height=max_height
        self.pixbuf = self.pixbuf.scale_simple(width, height, gtk.gdk.INTERP_HYPER)
        
        self.image.set_from_pixbuf(self.pixbuf)